package cc.rengu.redp.flowable.bizrule.rule;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * springboot启动时自动注册继承自BusiRule的类，所有规则都必须注解@Component注入Spring
 */
@Service
public class InitAllRules implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //获取Spring容器
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        //获取全部Renderer的衍生类
        Map<String, BusiRule> beans = applicationContext.getBeansOfType(BusiRule.class);

        for (Map.Entry<String, BusiRule> en : beans.entrySet()) {
            BusiRuleCommander.add(en.getValue().getClass().getSimpleName(), en.getValue());
        }
    }
}
